﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using System.Device.Location;
using System.Windows.Media;
using INdTFramework.Controls;
using Microsoft.Phone.Maps.Controls;
using System.Diagnostics;
using INdTFramework.Maps;

namespace MapSample
{
    public partial class MapWithPin : PhoneApplicationPage
    {
        // Object to assist in manipulating the map control.
        HelperMap helperMap;

        // List of coordinates
        List<GeoCoordinate> coordinatesNear = new List<GeoCoordinate>
        {
            new GeoCoordinate(-3.12154981215481, -60.0121984981601),
            new GeoCoordinate(-3.12198481491516, -60.0165443214131),
            new GeoCoordinate(-3.12502496536076, -60.0185100548110),
            new GeoCoordinate(-3.12198485215498, -60.0146511612131),
            new GeoCoordinate(-3.12781292164515, -60.0136065262659),
            new GeoCoordinate(-3.12872195425814, -60.0193521616521),
            new GeoCoordinate(-3.12942781374461, -60.0112452694859),
            new GeoCoordinate(-3.12975742134942, -60.0132962648214),
            new GeoCoordinate(-3.12872116427942, -60.0161876157432),
            new GeoCoordinate(-3.12214943548824, -60.0123521654062),
            new GeoCoordinate(-3.12854319667214, -60.0122503256528)
        };

        int index = 0;

        public MapWithPin()
        {
            InitializeComponent();
        }

        private void GoToMainPage_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Creation of an instance of the class HelperMap with the map control that is inserted into the view.
            helperMap = new HelperMap(MyMap);

            // Creation of coordinates
            var coordinates = new GeoCoordinate(-3.12702496536076, -60.0169777404517);

            // Image that represents the marker.
            var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin1.png", UriKind.Relative)) };

            // Marker position in relation its coordinates.
            var positionOrigin = new Point(0.5, 0.5);

            // Addition of the marker on the map control.
            helperMap.AddMarker(coordinates, image, positionOrigin);
        }

        private void AddPin_Click(object sender, EventArgs e)
        {
            if (index < coordinatesNear.Count)
            {
                // Image that represents the marker.
                var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin2.png", UriKind.Relative)) };

                // Instantiation of the object of type MapOverlay to insert the marker information.
                var overlay = new MapOverlay();
                overlay.GeoCoordinate = coordinatesNear[index];
                overlay.Content = image;
                overlay.PositionOrigin = new Point(0.5, 1);

                // Configuring the tap event for the user to have the option to remove a marker.
                image.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>((senderTap, obj) =>
                    {
                        var result = MessageBox.Show("Are you sure you want to delete this pin?", "Attention", MessageBoxButton.OKCancel);

                        if (result == MessageBoxResult.OK)
                        {
                            // Removal of a marker through the object that contains your information.
                            helperMap.RemoveMarker(overlay);
                        }
                    });

                // Adding the object, which contains the information of the marker, on the map control.
                helperMap.AddMarker(overlay);

                index++;

                // After adding markers to all coordinates of coordinatesNear list, ApplicationBar options are disabled.
                if (index == coordinatesNear.Count)
                {
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).IsEnabled = false;
                    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).IsEnabled = false;
                }
            }
            
        }

        private void AllPins_Click(object sender, EventArgs e)
        {
            // Instaciação a list of objects of type MapOverlay that will contain the characteristics of the markers.
            var list = new List<MapOverlay>();

            foreach (var coord in coordinatesNear)
            {
                // Image that represents the marker.
                var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin2.png", UriKind.Relative)) };

                // Configuring the tap event for the user to have the option to remove a marker.
                image.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>((senderTap, obj) =>
                {
                    var result = MessageBox.Show("Are you sure you want to delete this pin?", "Attention", MessageBoxButton.OKCancel);

                    if (result == MessageBoxResult.OK)
                    {
                        // Removal of a marker by its coordinates.
                        helperMap.RemoveMarker(coord);
                    }
                });

                // Instantiation of the object of type MapOverlay to insert the marker information.
                var overlay = new MapOverlay();
                overlay.GeoCoordinate = coord;
                overlay.Content = image;
                overlay.PositionOrigin = new Point(0.5, 1);

                // Adding an object that represents a new marker.
                list.Add(overlay);
            }

            //Adding a list of markers on the map control.
            helperMap.AddMarkers(list);

            // After adding markers to all coordinates of coordinatesNear list, ApplicationBar options are disabled.
            ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).IsEnabled = false;
            ((ApplicationBarMenuItem)ApplicationBar.MenuItems[1]).IsEnabled = false;

        }
    }
}